#include "Ship.h"
#include <stdio.h>
#include <stdlib.h>

Ship::Ship()
{
	name = "Unknown";
	type = 0;
	hitbox = 0;
	startPositionX = 0;
	startPositionY = 0;
	direction = 0;
}

Ship::Ship(int shipType, Coordinate &startCoords, int startDirection)
{
	if(shipType == 0)
	{
		name = "Patrol Boat";
		type = shipType;
		hitbox = 0x3;
		startPositionX = startCoords.getX();
		startPositionY = startCoords.getY();
		direction = startDirection;
		coords.push_back(startCoords);
		if(startDirection == 0)
		{
			for(int i = 1; i < 2; i++)
			{
				coords.push_back(*(new Coordinate(startCoords.getX(), startCoords.getY()-i)));
			}

		}
		if(startDirection == 1)
		{
			for(int i = 1; i < 2; i++)
			{
				coords.push_back(*(new Coordinate(startCoords.getX()+i, startCoords.getY())));
			}

		}
		if(startDirection == 2)
		{
			for(int i = 1; i < 2; i++)
			{
				coords.push_back(*(new Coordinate(startCoords.getX(), startCoords.getY()+i)));
			}
		}
		if(startDirection == 3)
		{
			for(int i = 1; i < 2; i++)
			{
				coords.push_back(*(new Coordinate(startCoords.getX()-i, startCoords.getY())));
			}

		}

		
	}
	else if(shipType == 1)
	{
		name = "Destroyer";
				type = shipType;
				hitbox = 0x7;
				startPositionX = startCoords.getX();
				startPositionY = startCoords.getY();
				direction = startDirection;
				coords.push_back(startCoords);
				if(startDirection == 0)
				{
					for(int i = 1; i < 3; i++)
					{
						coords.push_back(*(new Coordinate(startCoords.getX(), startCoords.getY()-i)));
					}

				}
				if(startDirection == 1)
				{
					for(int i = 1; i < 3; i++)
					{
						coords.push_back(*(new Coordinate(startCoords.getX()+i, startCoords.getY())));
					}

				}
				if(startDirection == 2)
				{
					for(int i = 1; i < 3; i++)
					{
						coords.push_back(*(new Coordinate(startCoords.getX(), startCoords.getY()+i)));
					}
				}
				if(startDirection == 3)
				{
					for(int i = 1; i < 3; i++)
					{
						coords.push_back(*(new Coordinate(startCoords.getX()-i, startCoords.getY())));
					}

				}


	}
	else if(shipType == 2)
	{
		name = "Submarine";
				type = shipType;
				hitbox = 0x7;
				startPositionX = startCoords.getX();
				startPositionY = startCoords.getY();
				direction = startDirection;
				coords.push_back(startCoords);
				if(startDirection == 0)
				{
					for(int i = 1; i < 3; i++)
					{
						coords.push_back(*(new Coordinate(startCoords.getX(), startCoords.getY()-i)));
					}

				}
				if(startDirection == 1)
				{
					for(int i = 1; i < 3; i++)
					{
						coords.push_back(*(new Coordinate(startCoords.getX()+i, startCoords.getY())));
					}

				}
				if(startDirection == 2)
				{
					for(int i = 1; i < 3; i++)
					{
						coords.push_back(*(new Coordinate(startCoords.getX(), startCoords.getY()+i)));
					}
				}
				if(startDirection == 3)
				{
					for(int i = 1; i < 3; i++)
					{
						coords.push_back(*(new Coordinate(startCoords.getX()-i, startCoords.getY())));
					}

				}


	}
	else if(shipType == 3)
	{
		name = "Battleship";
				type = shipType;
				hitbox = 0xF;
				startPositionX = startCoords.getX();
				startPositionY = startCoords.getY();
				direction = startDirection;
				coords.push_back(startCoords);
				if(startDirection == 0)
				{
					for(int i = 1; i < 4; i++)
					{
						coords.push_back(*(new Coordinate(startCoords.getX(), startCoords.getY()-i)));
					}

				}
				if(startDirection == 1)
				{
					for(int i = 1; i < 4; i++)
					{
						coords.push_back(*(new Coordinate(startCoords.getX()+i, startCoords.getY())));
					}

				}
				if(startDirection == 2)
				{
					for(int i = 1; i < 4; i++)
					{
						coords.push_back(*(new Coordinate(startCoords.getX(), startCoords.getY()+i)));
					}
				}
				if(startDirection == 3)
				{
					for(int i = 1; i < 4; i++)
					{
						coords.push_back(*(new Coordinate(startCoords.getX()-i, startCoords.getY())));
					}

				}


	}
	else if(shipType == 5)
	{
		name = "Aircraft Carrier";
				type = shipType;
				hitbox = 0x1F;
				startPositionX = startCoords.getX();
				startPositionY = startCoords.getY();
				direction = startDirection;
				coords.push_back(startCoords);
				if(startDirection == 0)
				{
					for(int i = 1; i < 5; i++)
					{
						coords.push_back(*(new Coordinate(startCoords.getX(), startCoords.getY()-i)));
					}

				}
				if(startDirection == 1)
				{
					for(int i = 1; i < 5; i++)
					{
						coords.push_back(*(new Coordinate(startCoords.getX()+i, startCoords.getY())));
					}

				}
				if(startDirection == 2)
				{
					for(int i = 1; i < 5; i++)
					{
						coords.push_back(*(new Coordinate(startCoords.getX(), startCoords.getY()+i)));
					}
				}
				if(startDirection == 3)
				{
					for(int i = 1; i < 5; i++)
					{
						coords.push_back(*(new Coordinate(startCoords.getX()-i, startCoords.getY())));
					}

				}


	}
}


void Ship::hit(std::list<Coordinate>::iterator shipIterator, Player *player)
{
	int tempX = abs(shipIterator->getX() - startPositionX);
	int tempY = abs(shipIterator->getY() - startPositionY);
	int temp = tempX + tempY;

	if(temp == 0)
	{
		hitbox = hitbox - 0x1;
	}
	else if(temp == 1)
		{
		hitbox = hitbox - 0x2;
		}
	else if(temp == 2)
		{
		hitbox = hitbox - 0x4;
		}
	else if(temp == 3)
		{
		hitbox = hitbox - 0x8;
		}
	else if(temp == 4)
		{
		hitbox = hitbox - 0x10;
		}

	if(hitbox == 0)
	{

	}

}

